package cn.edu.jsu.official.controller;

import cn.edu.jsu.official.pojo.dto.UserInfo;
import cn.edu.jsu.official.pojo.vo.CommonResult;
import cn.edu.jsu.official.pojo.vo.MemberVO;
import cn.edu.jsu.official.pojo.vo.PageModel;
import cn.edu.jsu.official.service.VisitorService;
import com.alibaba.fastjson.JSONObject;
import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSDownloadStream;
import com.mongodb.client.gridfs.model.GridFSFile;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsResource;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Mo
 * @createTime 2022/3/2 23:07
 * @description
 */
@RestController
@RequestMapping("/visitor")
public class VisitorController {
    @Autowired
    private VisitorService visitorService;

    @Autowired
    private GridFsTemplate gridFsTemplate;

    @Autowired
    private GridFSBucket gridFSBucket;

    @GetMapping("/queryAllProjectAchievement")
    public CommonResult queryAllProjectAchievement() {
        return visitorService.queryAllProjectAchievement();
    }

    @GetMapping("/queryAllContestAchievement")
    public CommonResult queryAllContestAchievement() {
        return visitorService.queryAllContestAchievement();
    }

    @GetMapping("/queryAllProjectInProgress")
    public CommonResult queryAllProjectInProgress() {
        return visitorService.queryAllProjectInProgress();
    }

    @GetMapping("/queryAllCopyright")
    public CommonResult queryAllCopyright() {
        return visitorService.queryAllCopyright();
    }

    @GetMapping("/queryAllMemberInfo")
    public CommonResult queryAllMemberInfo() {
        return visitorService.queryAllMemberInfo();
    }

    @GetMapping("/queryAllGoalInfo")
    public CommonResult queryAllGoalInfo() {
        return visitorService.queryAllGoalInfo();
    }

    @GetMapping("/queryAllContestInfo")
    public CommonResult queryAllContestInfo(){
        return visitorService.queryAllContestInfo();
    }

    @GetMapping("/queryAllHistoryProcess")
    public CommonResult queryAllHistoryProcess() {
        return visitorService.queryAllHistoryProcess();
    }

    @PostMapping("/queryArticle")
    @ApiOperation("查询文章")
    public CommonResult queryArticle(@RequestBody String articleId, HttpServletRequest request, HttpServletResponse response) {
        System.out.println(articleId);
        articleId = articleId.replace("\"", "");
        System.out.println(articleId);
        return visitorService.queryArticle(articleId);
    }
    @PostMapping("/queryImage")
    @ApiOperation("查询图片")
    public CommonResult queryImage(@RequestBody String imageId, HttpServletRequest request, HttpServletResponse response) {
        imageId = imageId.replace("\"", "");
        return visitorService.queryImage(imageId, request, response);
    }

    @GetMapping("/countAchievements")
    @ApiOperation("查询成就数量")
    public CommonResult countAchievements() {
        return visitorService.countAchievements();
    }

    @GetMapping("/queryAllMemberWithImage")
    @ApiOperation("按条件查询成员信息和头像")
    public CommonResult queryAllMemberWithImage(@RequestParam Integer condition) {
        return visitorService.queryAllMemberWithImage(condition);
    }

    @PostMapping("/countAchievementNumberGroupByYears")
    @ApiOperation("查询最近几年成就数量")
    public CommonResult countAchievementNumberGroupByYears(@RequestBody Integer queryType) {
//        Integer recentYearLimit = Integer.valueOf(JSONObject.parseObject(data).getString("recentYearLimit"));
//        Integer queryType = Integer.valueOf(JSONObject.parseObject(data).getString("queryType"));
        return visitorService.countAchievementNumberGroupByYears(queryType);
    }

    @GetMapping("/queryAllTechStackInfo")
    @ApiOperation("查询所有技术栈信息")
    public CommonResult queryTechStackInfo() {
        return visitorService.queryAllTechStackInfo();
    }

    @GetMapping("/queryChooseMember")
    @ApiOperation("查询被展示的成员")
    public CommonResult queryChooseMember(){
        return visitorService.queryChooseMember();
    }
//
//    @GetMapping("/getStatics")
//    @ApiOperation("获取首页展示数据")
//    public CommonResult getStatics

//    @PostMapping("/test")
//    public CommonResult test(@RequestParam("data") String data) throws IOException {
//        Query query = new Query(Criteria.where("_id").is(data));
//        GridFSFile one = gridFsTemplate.findOne(query);
//        GridFSDownloadStream gridFS = gridFSBucket.openDownloadStream(one.getObjectId());
//        GridFsResource resource = new GridFsResource(one, gridFS);
////        StreamingResponseBody streamingResponseBody = outputStream -> {
////            IOUtils.copy(resource.getInputStream(), outputStream);
////        };
////        System.out.println(streamingResponseBody);
//        byte[] bytes = IOUtils.toByteArray(resource.getInputStream());
//        String fileContent = new String(bytes, StandardCharsets.UTF_8);
//        System.out.println(fileContent);
//        return new CommonResult(200, "查询成功", fileContent);
//    }
}
